// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('starter', ['ionic', 'wj'])

    .controller('GuessNumberCtrl', ['$rootScope', '$scope',
        function ($rootScope, $scope) {
            var session = new gdg.GuessNumber();

            $scope.report = 'grid';
            $scope.reportSource = new wijmo.collections.CollectionView(session.histories);

            $scope.value = undefined;
            $scope.answer = session.answer;
            $scope.isValueCorrect = false;

            $scope.check = function () {
                $scope.isValueCorrect = session.check($scope.value);

                // refresh collection view to let ui updated
                $scope.reportSource.refresh();
            };
            $scope.count = function () {
                return session.histories.length;
            };
            $scope.lastMessage = function () {
                return session.histories.length > 0 ? session.histories[0].message : "n/a";
            };

            $rootScope.isAnswerVisible = false;
            $rootScope.newGame = function () {
                $scope.value = undefined;
                $scope.isValueCorrect = false;
                session.reset();
                $scope.answer = session.answer;
                $scope.reportSource.refresh();
            };
        }])

    .run(function ($ionicPlatform) {
        $ionicPlatform.ready(function () {
            // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
            // for form inputs)
            if (window.cordova && window.cordova.plugins.Keyboard) {
                cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
            }
            if (window.StatusBar) {
                StatusBar.styleDefault();
            }
        });
    });
